package util.math;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

public class MyMath {
	
	List<Double> lenList;
	int size;
	
	public int getSize() {
		return size;
	}
	
	public MyMath(List<Double> lenList) {
		
		this.lenList = lenList;
		this.size = this.lenList.size();
		Collections.sort(this.lenList);
		
	}
	
	public double getDecimal(double num) {
		
		if (Double.isNaN(num)) {
			return 0.0;
		}
		
		BigDecimal bd = new BigDecimal(num);
		num = bd.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
		return num;
	}
	
	
	
	
	
	/*
	 * 最小值
	 */

	public double getMin() {
		return lenList.get(0);
	}
	
	/*
	 * 最小值
	 */
	public double getMax() {
		return lenList.get(size-1);
	}
	
	/*
	 * 求和
	 */
	public double getSum() {
		double sum = 0;
		for (double length : lenList) {
			sum  += length;
		}
		
		return sum;
	}
	
	/*
	 * 平均数
	 */
	
	public double getAveD() {
		return getDecimal(getAve());
	}
	
	public double getAve () {
		double sum = getSum();
		double ave = sum/size;
		return ave;
	}
	
	/*
	 * 中值
	 */
	public double getMedian() {
		int m = size/2;
		double median;
		double temp;
		if (size%2 == 0) {
			temp = lenList.get(m-1) + lenList.get(m);
			median = temp/2;
		}else {
			median = lenList.get(m);
		}
		return getDecimal(median);
	}
	
	/*
	 * 方差计算
	 */
	public double getVarianceD() {
		return getDecimal(getVariance());
	}
	
	public double getVariance() {
		double ave = getAve();
		double minus;
		double powSum = 0;
		for (double len : lenList) {
			minus = Math.pow((len - ave), 2);
			powSum += minus;
		}
		
		return powSum/size;
		
	}
	/*
	 * 标准差计算
	 */
	public double getStandard() {
		double variance = getVariance();
		double standard = Math.sqrt(variance);
		return standard;
	}
	
	public double getStandardD() {
		return getDecimal(getStandard());
	}
	
	/*
	 * 计算四阶中心距
	 */
	
	public double getNCenter(double n) {
		double ave = getAve();
		double minus;
		double nCenter = 0;
		for (double len : lenList) {
				minus = Math.pow(len - ave, n);
				nCenter += minus;
			}
		return nCenter;
	}
	
	/*
	 * 计算偏度系数
	 */
	public double getSkew() {
		double sd = getStandard();
		double threeCenter = getNCenter(3.0);
		double skew = (size * threeCenter)/((size-1) * (size - 2) * Math.pow(sd, 3));
		
		return skew;
	}
	
	public double getSkewD() {
		return getDecimal(getSkew());
	}
	
	/*
	 * 计算峰度值
	 */
	
	public double getKurt() {
		
		double sd = getStandard();
		double fourCenter = getNCenter(4.0);
		double kurt = (fourCenter/(size - 1))/Math.pow(sd, 4) - 3;
		return kurt;
		
	}
	
	public double getKurtD() {
		return getDecimal(getKurt());
	}
	

	
	

}























